import { reroute } from "./navigation/reroute.js";
import { formatErrorMessage } from "./applications/app-errors.js"; //错误信息报错log函数
import { setUrlRerouteOnly } from "./navigation/navigation-events.js";
import { isInBrowser } from "./utils/runtime-environment.js"; //判断是否浏览器环境

let started = false;

/**
 * 启动Single-spa项目
 * @param {*} opts 配置信息
 * @description 进来将started设置为true 为的是记录运行时间是否超过阈值 之后... 然后判断是否在浏览器环境并运行
 * 在调用start之前，应用被加载了，但是不会初始化，不会挂载或者卸载。
 * 调用这个函数，就意味着要开始挂载，就会开始激活应用里的代码
 * @see https://single-spa.js.org/docs/api#start
 */
export function start(opts) {
  started = true;
  // 是否不希望浏览器路由变化后，single-spa重新路由
  if (opts && opts.urlRerouteOnly) {
    setUrlRerouteOnly(opts.urlRerouteOnly);
  }
  // 判断是否在浏览器环境下
  if (isInBrowser) {
    reroute();
  }
}
// 是否已经启动
export function isStarted() {
  return started;
}

// 在浏览器环境下运行超5秒后打出警告log
if (isInBrowser) {
  setTimeout(() => {
    if (!started) {
      console.warn(
        formatErrorMessage(
          1,
          __DEV__ &&
            `singleSpa.start() has not been called, 5000ms after single-spa was loaded. Before start() is called, apps can be declared and loaded, but not bootstrapped or mounted.`
        )
      );
    }
  }, 5000);
}
